package com.zhuiyun.project.api.system.dict.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuiyun.project.api.system.dict.entity.DictData;
import com.zhuiyun.project.api.system.dict.mapper.DictDataMapper;
import com.zhuiyun.project.api.system.dict.service.DictDataService;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.ObjectUtils;
import com.zhuiyun.project.utils.RedisUtil;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName DictDataServiceImpl
 * @Description TODO 字典详情接口实现类
 * @Author gpd
 * @Date 2023-06-16 上午 10:08
 **/
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements DictDataService {

    @Autowired
    DictDataMapper dictDataMapper;

    /**
     * @Author gpd
     * @Description 根据字典类型查询字典数据信息
     * @Date 2023-06-16 上午 10:11
     * @Return CommonResult

     */
    @Override
    public List<DictData> type(String dictType) {
        List<DictData> dictDatas = RedisUtil.getDictCache(dictType);
        if(dictDatas!=null&&dictDatas.size()>0){
            return dictDatas;
        }
        dictDatas = dictDataMapper.selectDictDataByType(dictType);
        if (dictDatas!=null)
        {
            RedisUtil.set(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    /**
     * @Author gpd
     * @Description 字典详情分页查询
     * @Date 2023-06-19 下午 02:27
     * @Return PageResult

     */
    @Override
    public IPage<DictData> list(PageRequest request, DictData dictData) {
        // 创建查询构造器
        QueryWrapper<DictData> dictDataEntityQueryWrapper = new QueryWrapper<>();
        // 设置查询条件
        if (dictData.getDictType() != null) {
            dictDataEntityQueryWrapper.eq("dict_type", dictData.getDictType());
        }
        if (dictData.getDictLabel() != null) {
            dictDataEntityQueryWrapper.eq("dict_label", dictData.getDictLabel());
        }
        if (dictData.getStatus() != null) {
            dictDataEntityQueryWrapper.eq("status", dictData.getStatus());
        }
        // 设置分页规则
        Page<DictData> dictDataEntityPage = new Page<>(request.getPageNum(), request.getPageSize());
        // 根据分页规则和条件进行查询
        IPage<DictData> dictDataEntityIPage = dictDataMapper.selectPage(dictDataEntityPage, dictDataEntityQueryWrapper);
        return dictDataEntityIPage;
    }

    /**
     * @Author gpd
     * @Description 查询字典数据详细(用于修改按钮的回显)
     * @Date 2023-06-19 下午 03:03
     * @Return CommonResult

     */
    @Override
    public DictData dictCode(Long dictCode) {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * @Author gpd
     * @Description 新增字典详情
     * @Date 2023-06-19 下午 03:17
     * @Return CommonResult

     */
    @Override
    public Integer addDictData(DictData dictData) {
        int row = dictDataMapper.insertDictData(dictData);
        if(row>0){
            // 查询新增进去的字典详情信息
            List<DictData> dictDataEntities = dictDataMapper.selectDictDataByType(dictData.getDictType());
            // 以字典详情为键,dictDataEntities为值存进reids缓存中
            RedisUtil.setDictCache(dictData.getDictType(),dictDataEntities);
        }
        return row;
    }

    /**
     * @Author gpd
     * @Description 修改字典详情
     * @Date 2023-06-19 下午 03:23
     * @Return CommonResult

     */
    @Override
    public Integer updateDictData(DictData dictData) {
        int row = dictDataMapper.updateDictData(dictData);
        if(row>0){
            // 查询新增进去的字典详情信息
            List<DictData> dictDataEntities = dictDataMapper.selectDictDataByType(dictData.getDictType());
            // 以字典详情为键,dictDataEntities为值存进reids缓存中
            RedisUtil.setDictCache(dictData.getDictType(),dictDataEntities);
        }
        return row;
    }

    /**
     * @Author gpd
     * @Description 删除字典详情
     * @Date 2023-06-19 下午 03:31
     * @Return CommonResult

     */
    @Override
    public CommonResult deleteDictData(Long[] dictCode) {
        if(ObjectUtils.isNotEmpty(dictCode)) {
            for (Long code : dictCode){
                // 查询要删除的字典详情
                DictData dictData = dictDataMapper.selectDictDataById(code);
                int row = dictDataMapper.deleteDictDataById(code);
                if(row>0){
                    // 根据字典类型名称查询剩余的字典详情
                    List<DictData> dictDataEntities = dictDataMapper.selectDictDataByType(dictData.getDictType());
                    // 以字典类型名称为键,剩余的字典详情为值放进redis中
                    RedisUtil.setDictCache(dictData.getDictType(),dictDataEntities);
                    return CommonResult.ok(EmErrorCode.DELETE_OK);
                }
                else{
                    return CommonResult.error(EmErrorCode.DELETE_ERROR);
                }
            }
        }
        else{
            return CommonResult.error(EmErrorCode.DELETE_ERROR);
        }
        return null;
    }
}
